package main

import (
	"context"
	"fmt"

	base "2024-fall-etohlcin3-backend/cmd/kitex_gen/industics/blockchain/base"

	"github.com/hyperledger/fabric-gateway/pkg/client"
)

// PurchaseServiceImpl implements the last service interface defined in the IDL.
type PurchaseServiceImpl struct{}

var PurchaseOrg *client.Contract

// GetPurchaseRequire implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) GetPurchaseRequire(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	// fmt.Println(req)
	evaluateResult, err := readForKey(req.GetId())
	// evaluateResult := "ooops no impl"
	if err != nil {
		resp = &base.BaseGetResp{
			Code:     "404",
			Msg:      "not exist",
			JsonData: err.Error(),
		}
		return
	}
	fmt.Println(req.GetId())
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetPurchaseOrder implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) GetPurchaseOrder(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForKey(req.GetId())
	// evaluateResult := "ooops no impl"
	if err != nil {
		resp = &base.BaseGetResp{
			Code:     "404",
			Msg:      "not exist",
			JsonData: err.Error(),
		}
		return
	}
	fmt.Println(req.GetId())
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetPurchaseContract implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) GetPurchaseContract(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForKey(req.GetId())
	// evaluateResult := "ooops no impl"
	if err != nil {
		resp = &base.BaseGetResp{
			Code:     "404",
			Msg:      "not exist",
			JsonData: err.Error(),
		}
		return
	}
	fmt.Println(req.GetId())
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetPurchaseRequireHistory implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) GetPurchaseRequireHistory(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readHistoryForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetPurchaseOrderHistory implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) GetPurchaseOrderHistory(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readHistoryForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetPurchaseContractHistory implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) GetPurchaseContractHistory(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readHistoryForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetPurchaseRequireByRange implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) GetPurchaseRequireByRange(ctx context.Context, req *base.BaseRangeGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForRange(req.GetRangeStart(), req.GetRangeEnd())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetPurchaseOrderByRange implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) GetPurchaseOrderByRange(ctx context.Context, req *base.BaseRangeGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForRange(req.GetRangeStart(), req.GetRangeEnd())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetPurchaseContractByRange implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) GetPurchaseContractByRange(ctx context.Context, req *base.BaseRangeGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForRange(req.GetRangeStart(), req.GetRangeEnd())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// CreatePurchaseRequire implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) CreatePurchaseRequire(ctx context.Context, req *base.BasePostReq) (resp *base.BasePostResp, err error) {
	_, err = createForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePostResp{
		Code: "201",
		Msg:  "ok",
	}
	return
}

// CreatePurchaseOrder implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) CreatePurchaseOrder(ctx context.Context, req *base.BasePostReq) (resp *base.BasePostResp, err error) {
	_, err = createForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePostResp{
		Code: "201",
		Msg:  "ok",
	}
	return
}

// CreatePurchaseContract implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) CreatePurchaseContract(ctx context.Context, req *base.BasePostReq) (resp *base.BasePostResp, err error) {
	_, err = createForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePostResp{
		Code: "201",
		Msg:  "ok",
	}
	return
}

// UpdatePurchaseRequire implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) UpdatePurchaseRequire(ctx context.Context, req *base.BasePutReq) (resp *base.BasePutResp, err error) {
	_, err = updateForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePutResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// UpdatePurchaseOrder implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) UpdatePurchaseOrder(ctx context.Context, req *base.BasePutReq) (resp *base.BasePutResp, err error) {
	_, err = updateForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePutResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// UpdatePurchaseContract implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) UpdatePurchaseContract(ctx context.Context, req *base.BasePutReq) (resp *base.BasePutResp, err error) {
	_, err = updateForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePutResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// DeletePurchaseRequire implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) DeletePurchaseRequire(ctx context.Context, req *base.BaseDeleteReq) (resp *base.BaseDeleteResp, err error) {
	_, err = deleteForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseDeleteResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// DeletePurchaseOrder implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) DeletePurchaseOrder(ctx context.Context, req *base.BaseDeleteReq) (resp *base.BaseDeleteResp, err error) {
	_, err = deleteForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseDeleteResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// DeletePurchaseContract implements the PurchaseServiceImpl interface.
func (s *PurchaseServiceImpl) DeletePurchaseContract(ctx context.Context, req *base.BaseDeleteReq) (resp *base.BaseDeleteResp, err error) {
	_, err = deleteForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseDeleteResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

func readForKey(formID string) ([]byte, error) {
	return PurchaseOrg.EvaluateTransaction("ReadForm", formID)
}
func readHistoryForKey(formID string) ([]byte, error) {
	return PurchaseOrg.EvaluateTransaction("ReadFormHistoryForKey", formID)
}
func readForRange(formIDStart string, formIDEnd string) ([]byte, error) {
	return PurchaseOrg.EvaluateTransaction("ReadFormsByRange", formIDStart, formIDEnd)
}
func createForKey(formID string, formJSON string) ([]byte, error) {
	return PurchaseOrg.SubmitTransaction("CreateForm", formID, formJSON)
}
func updateForKey(formID string, formJSON string) ([]byte, error) {
	return PurchaseOrg.SubmitTransaction("UpdateForm", formID, formJSON)
}
func deleteForKey(formID string) ([]byte, error) {
	return PurchaseOrg.SubmitTransaction("DeleteForm", formID)
}
